﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using Model;
using System.Drawing;
using System.Threading;

namespace OpenLeuvenPortaal
{
    public partial class OpenLeuvenALT : System.Web.UI.MasterPage
    {

        string username = String.Empty;
        string password = String.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["postbackint"] != null)
            {
                if (int.Parse(Session["postbackint"].ToString()) == 1)
                {
                    string gbrnaam = Session["gebruikersnaam"].ToString();
                    int gbr = int.Parse(Session["userid"].ToString());
                    bool LoggedIn = false;
                    Authentication auth = new Authentication();
                    LoggedIn = auth.LoginCheck(gbrnaam, gbr);


                    if (LoggedIn == true)
                    {
                        Label1.Text = "Welkom " + Session["gebruikersnaam"];
                        Label2.Visible = false;
                        gbrTxt.Visible = false;
                        WachtwoordTxt.Visible = false;
                        LoginBtn.Visible = false;
                        RegLink.Visible = false;

                        int rolid = auth.Getuser(gbrnaam).RolID;

                        if (rolid == 3)
                        {
                            MenuItem developerpage = new MenuItem();
                            developerpage.Text = "DEVELOPER AREA";
                            developerpage.NavigateUrl = "~/Views/StartPagina/DeveloperHomePage.aspx";

                            NavigationMenu.Items.Add(developerpage);
                        }

                        MenuItem userprofile = new MenuItem();
                        NavigationMenu.Items.Add(userprofile);
                        userprofile.Text = "User Profile";
                        userprofile.NavigateUrl = "~/Views/StartPagina/UserProfile.aspx";

                        MenuItem logoutbtn = new MenuItem();
                        NavigationMenu.Items.Add(logoutbtn);
                        logoutbtn.Text = "Log out";
                        logoutbtn.NavigateUrl = "~/Views/StartPagina/logout.aspx";
                    }
                    else if (LoggedIn == false)
                    {
                        Label1.Text = "Gebruikersnaam";
                        Label2.Visible = true;
                        gbrTxt.Visible = true;
                        WachtwoordTxt.Visible = true;
                        LoginBtn.Visible = true;
                        RegLink.Visible = true;
                    }
                }
            }
        }



        protected void LoginBtn_click(object sender, EventArgs e)
        {
            //bolt nit
        }

        protected void LoginBtn_Click1(object sender, EventArgs e)
        {
            if (gbrTxt.Text != String.Empty && WachtwoordTxt.Text != String.Empty)
            {
                Authentication auth = new Authentication();
                HashHandle hash = new HashHandle();
                bool userclear = false;

                username = gbrTxt.Text;
                password = hash.GenerateHash(WachtwoordTxt.Text);

                userclear = auth.CheckLogin(username, password);

                if (userclear == true)
                {
                    Gebruiker gbr = auth.Getuser(username);

                    Session["userid"] = gbr.UserID;
                    Session["gebruikersnaam"] = gbr.Gebruikersnaam;
                    Session["RoleID"] = gbr.RolID;
                    Session["LoggedIn"] = true;
                    Session["postbackint"] = 1;
                    Label1.Text = "Welkom" + Session["gebruikersnaam"];
                    Label2.Visible = false;
                    gbrTxt.Visible = false;
                    WachtwoordTxt.Visible = false;
                    LoginBtn.Visible = false;
                    RegLink.Visible = false;
                    Response.Redirect("StartPagina.aspx");
                }
            }
            else
            {
                System.Drawing.Color tempcolor = RegLink.ForeColor;
                string tempReglink = RegLink.Text;
                RegLink.ForeColor = Color.Red;
                RegLink.Text = "Gebruikersnaam en wachtwoord moeten ingevuld zijn.";
                Thread.Sleep(2000);
                RegLink.ForeColor = tempcolor;
                RegLink.Text = tempReglink;
            }
        }
    }
}